package com.knife.blog.web.action;

import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.BlogUser;
import com.knife.blog.logic.BlogNewsDocService;
import com.knife.blog.logic.BlogUserService;
import com.knife.blog.logic.CurrentUser;
import com.knife.blog.logic.impl.BlogCoreServiceImpl;
import com.knife.blog.logic.impl.BlogResourceServiceImpl;
import com.knife.blog.logic.impl.BlogUserServiceImpl;
import com.knife.blog.logic.impl.FriendLinkServiceImpl;
import com.knife.blog.logic.impl.PhotoServiceImpl;
import com.knife.util.CommUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.AbstractCmdAction;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class ManageAction extends AbstractCmdAction {
    private BlogUserService userService = BlogUserServiceImpl.getInstance();
    private static BlogNewsDocService blogDocService = BlogCoreServiceImpl.getInstance();
    private BlogInfo blog;
    private BlogUser bloguser;

    @Override
    public Object doBefore(WebForm form, Module module) {
        IActiveUser user = CurrentUser.getActiveUser();
        if (user == null) {
            // 请先登录
            form.addResult("url", "manage.do");
            form.addResult("msg", "请先登录！");
            return module.findPage("login");
        }
        blog = userService.getUserBlog(user.getUserName());
        if (blog == null) {
            form.addResult("msg", "你还没申请Blog服务，请先申请开通Blog服务！");
            form.addResult("username", user.getUserName());
            return module.findPage("register");
        }
        return super.doBefore(form, module);
    }

    public Page doInit(WebForm form, Module module) {
        IActiveUser user = CurrentUser.getActiveUser();
        if (user == null) {
            // 请先登录
            form.addResult("url", "manage.do");
            form.addResult("msg", "请先登录！");
            return module.findPage("login");
        }
        if (com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(user.getUserName())) {
            form.addResult("isAdmin", "true");
            return module.findPage("adminMain");
        } else {
            return doStatusInfo(form,module);
        }
    }

    public Page doShowMenu(WebForm form, Module module) throws Exception {
        if (com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(
                CurrentUser.getActiveUser().getUserName())) {
            form.addResult("isAdmin", "true");
            System.out.println(com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(
                    CurrentUser.getActiveUser().getUserName()));
            return module.findPage("adminMenu");
        } else {
            return module.findPage("userMenu");
        }
    }

    public Page doStatusInfo(WebForm form, Module module) {
        int pageSize;
        pageSize = blog.getFirtpagenum();
        if (pageSize < 0) {
            pageSize = 15;
        }
        int currentPage = CommUtil.null2Int(form.get("page"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        IPageList pageList = blogDocService.queryBlogNewsDoc(blog.getUsername(), currentPage,
                pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        return module.findPage("userMain");
    }

    public Page doManage(WebForm form, Module module) throws Exception {
        return module.findPage("manage");
    }

    @Override
    public Object doAfter(WebForm form, Module module) {
        if (blog != null) {
        	bloguser=userService.getBlogUser(blog.getUsername());
            form.addResult("blog", blog);
            form.addResult("bloguser", bloguser);
            form.addResult("dirService", BlogCoreServiceImpl.getInstance());
            form.addResult("photoService", PhotoServiceImpl.getInstance());
            form.addResult("userService", userService);
            form.addResult("linkService",FriendLinkServiceImpl.getInstance());
            form.addResult("resourceService",BlogResourceServiceImpl.getInstance());
        }
        return super.doAfter(form, module);
    }
}
